package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;



import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;

/**
 * 文件操作
 * @author 非灭天
 *
 */
public class FileOperate {
	private static String[] af=null;
	public static String imageExt="bmp,BMP,JPG,jpg,JPEG,jpeg,gif,GIF,";
	public static String voiceExt="wav,WAV,MP3,mp3,amr,AMR,";
	public static String excelExt="XLS,xls,";
	/**
	 * 文件保存路径
	 */
	public static String excelSavePath = "./export.xls";
	
	/**
	 * 得到文件保存路径
	 * @return
	 */
	public static String getExcelSavePath() {
		return excelSavePath;
	}
    
	/**
	 * 设置文件保存路径
	 * @param excelSavePath
	 */
	public static void setExcelSavePath(String excelSavePath) {
		FileOperate.excelSavePath = excelSavePath;
	}

	/**
	 * 工作表总数
	 */
	private static int sheetIndexTest = 1;
	/**
	 * 最大行
	 */
	private int rowIndexTest = 4;
	/**
	 * 最大列
	 */
	private int colIndexTest = 6;

	
	
	/**
	 * 读取文件
	 * @param fname
	 * @return
	 */
	public static String readFile(String fname) {
		String content = "";
		try {
			BufferedReader br = new BufferedReader(new BufferedReader(new InputStreamReader(new FileInputStream(new File(fname)),"utf-8")));
			while (br.ready()) {
				String s = br.readLine();
				if("".equals(content))
				{
				  content=s;
				}else{
				  content += s ;
				}
			}
			br.close();
		} catch (Exception ex) {
		}

		return content;
	}
	/**
	 * 读取目录内所有文件
	 * @param strv 路径
	 * @return String[]
	 */
	public static String[] readAllFilesinDirectory(String strv) {
		File f = new File(strv);
		if(f.list()!=null){
		String fs[] = f.list();	
		af=new String[fs.length];
			for (int i = 0; i < fs.length; i++) {				
				File fv = new File(fs[i]);
				if (fv.canWrite()) {
					break;
				} else {
					try {
						System.out.println(f.getCanonicalPath() + fs[i]);
						af[i]=fs[i];
//						VskVmethodBody.readAllDirectory(f.getCanonicalPath() + fs[i]);						
					} catch (Exception e) {						
						e.printStackTrace();
					}
				}
			}
		}
		return af;
	}
	/**
	 * 传入二维数组 写成excel文件
	 * write excel to file
	 */
	public static void writeExcel(String[][] inArray) {
		try {
			if(inArray==null){
				inArray=new String[0][0];
			}
			// 创建新工作薄　
			// create new Excel workbook 　
			HSSFWorkbook workbook = new HSSFWorkbook();
			for (int sheetIndex = 0; sheetIndex < sheetIndexTest; sheetIndex++) {
				// 在工作薄中创建工作表 sheet X 　　
				// create new sheet in workbook
				HSSFSheet sheet = workbook.createSheet("sheet" + sheetIndex);
				// HSSFSheet sheet = workbook.createSheet();
				for (int rowIndex = 0; rowIndex < inArray.length; rowIndex++) {
					// 在工作表中创建行
					// create row in sheet
					HSSFRow row = sheet.createRow(rowIndex);
					for (int colIndex = 0; colIndex < inArray[0].length; colIndex++) {
						// 在索引rowIndex的位置创建单元格（左上端）
						// create column in row
						HSSFCell cell = row.createCell(colIndex);
						// 定义单元格为字符串类型 　　
						// set the cell data type
						cell.setCellType(HSSFCell.CELL_TYPE_STRING);
						// 在单元格中输入一些内容
						// set the cell data
						cell.setCellValue(inArray[rowIndex][colIndex]);
					}// end for colIndex
				}// end for rowIndex
			}// end for sheetIndex
			// 新建一输出文件流
			// new FileOutputStream
			FileOutputStream fOut = new FileOutputStream(excelSavePath);
			// 把相应的Excel 工作簿存盘
			// save the excel workbook
			workbook.write(fOut);
			fOut.flush();
			// 操作结束，关闭文件
			// finish operate close file
			fOut.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}// end method writeExcel

	/**
	 * 删除文件
	 * 
	 * @param fname
	 */
	public static void deleteFile(String fname) {
		// System.out.println(fname);
		(new File(fname)).delete();
	}
	
	public static String getDBConfig(String fname){		
		return FileOperate.readFile(fname);
		
	}
	
	/**
	 * 读取excel文件
	 * @param inFile 文件路径
	 * read excel file
	 */
	public static String[][] readExcel(String inFile) {
		String[][] tempArray=null;
		try {
			// read the workbook in file
			HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inFile));
			// get the sheet in workbook
			// HSSFSheet sheet = workbook.getSheet("Sheet0");
			//得到excel内工作表总数
			int sheetIndexTest=workbook.getNumberOfSheets();
			
			for (int sheetIndex = 0; sheetIndex < sheetIndexTest; sheetIndex++) {
				// you can use getSheetAt(int index) get the sheet in workbook
				// 也可用getSheetAt(int index)按索引引用
				HSSFSheet sheet = workbook.getSheetAt(sheetIndex);
				//得到工作表内行数
				int rowIndexTest=sheet.getPhysicalNumberOfRows();
				for (int rowIndex = 0; rowIndex < rowIndexTest; rowIndex++) {
					HSSFRow row = sheet.getRow(rowIndex);
					//得到列数
					int colIndexTest=row.getLastCellNum();
					for (int colIndex = 0; colIndex < colIndexTest; colIndex++) {
						HSSFCell cell = row.getCell(colIndex);
						if(tempArray==null){
							tempArray=new String[rowIndexTest][colIndexTest];
						}
						//System.out.print("["+rowIndex + ":" + colIndex + "||"	+ cell.getStringCellValue()+"]");
						tempArray[rowIndex][colIndex]=(String)(cell.getStringCellValue()==null?cell.getNumericCellValue():cell.getStringCellValue());
					}// end for colIndex
					//System.out.println();
				}// end for rowIndex
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return tempArray;

	}// end method readExcel
	
	/**
	 * 
	 * @param inFilePath
	 * @param outFilePath
	 * @param inWidth
	 * @param inHeight
	 */
	public static void changePictureSize(String inFilePath,String outFilePath,int inWidth,int inHeight){
		File src = new File(inFilePath) ;
        try {
            Image image = ImageIO.read(src) ;
            int width = inWidth ;
            int height = inHeight ;
            BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB) ;
            bufferedImage.getGraphics().drawImage(image, 0, 0, width, height,null) ;
            FileOutputStream fos = new FileOutputStream(outFilePath) ;
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos) ;
            encoder.encode(bufferedImage) ;
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
		
	}

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		Image ti=ImageIO.read(new File("g:/1.jpg"));
		System.out.println(ti.getWidth(null)+"||"+ti.getHeight(null));
	}//end main method

}//end class 
